//
//  CustomSlider.m
//  CoolTennisBall
//
//  Created by Coollang on 16/7/30.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "CustomSlider.h"

@interface CustomSlider ()
/** 滑块 */
@property (nonatomic, strong) CALayer *thumbImageLayer;
/** 渐变色块 */
@property (nonatomic, strong) CAGradientLayer *minTrackLayer;
@property (nonatomic, strong) CAGradientLayer *maxTrackLayer;

@end

@implementation CustomSlider

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 添加手势
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
        [self addGestureRecognizer:panGesture];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        // 添加手势
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
        [self addGestureRecognizer:panGesture];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
        [self addGestureRecognizer:tapGesture];
        
    }
    return self;
}

- (void)tapGestureAction:(UITapGestureRecognizer *)tap {
    CGPoint startP = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height);
    CGPoint curP = [tap locationInView:self];
    
    CGFloat postionY = startP.y - curP.y;
    if (postionY >= -2 && postionY <= self.bounds.size.height + 8) {
        [self updateProgroess:postionY];
    }
}

- (void)panGestureAction:(UIPanGestureRecognizer *)pan {
    
    CGPoint startP = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height);
    CGPoint curP = CGPointZero;
    
    if(pan.state == UIGestureRecognizerStateBegan){
        //1.获得点击的起始点
         curP = [pan locationInView:self];
        
         CGFloat postionY = startP.y - curP.y;
        if (postionY >= -2 && postionY <= self.bounds.size.height + 8) {
            [self updateProgroess:postionY];
        }
     
    }else if (pan.state == UIGestureRecognizerStateChanged){
        //2.求偏移量
         curP = [pan locationInView:self];
        CGFloat postionY = startP.y - curP.y;
        
        if (postionY >= -2 && postionY <= self.bounds.size.height + 8) {
            
            [self updateProgroess:postionY];;
        }
        
    }else if (pan.state == UIGestureRecognizerStateEnded){
        curP = [pan locationInView:self];
        CGFloat postionY = startP.y - curP.y;
        
        if (postionY >= -2 && postionY <= self.bounds.size.height + 8) {
            
            [self updateProgroess:postionY];;
        }
    }

}

- (void)updateProgroess:(CGFloat)postionY {
    
    if (postionY > self.bounds.size.height) {
        postionY = self.bounds.size.height;
    }
    
    if (postionY < 0) {
        postionY = 0;
    }
   
    self.thumbImageLayer.position = CGPointMake(self.bounds.size.width * 0.5, (CGRectGetHeight(self.frame) - postionY));
    self.minTrackLayer.frame = CGRectMake(0, CGRectGetHeight(self.frame) , CGRectGetWidth(self.frame),  - postionY);
    
    self.minTrackLayer.startPoint = CGPointMake(0.5,postionY / CGRectGetHeight(self.frame));
    
    self.value = postionY / CGRectGetHeight(self.frame);
    if (self.valueChange) {
        self.valueChange(self.value);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setGradientColor];
    [self setUpUI];
    
}
// 设置渐变色
- (void)setGradientColor {
    CAGradientLayer *maxTrackLayer = [[CAGradientLayer alloc] init];
    
    maxTrackLayer.colors = @[(__bridge id)[UIColor colorWithHex:0xc6c6c6].CGColor,(__bridge id)[UIColor colorWithHex:0x7b7b7b].CGColor];
    
    maxTrackLayer.startPoint = CGPointMake(0.5, 1);
    
    maxTrackLayer.endPoint = CGPointMake(0.5, 0);
    
    maxTrackLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.maxTrackLayer = maxTrackLayer;
    [self.layer addSublayer:maxTrackLayer];
    
    
    CAGradientLayer *minTrackLayer = [[CAGradientLayer alloc] init];
    
    minTrackLayer.colors = @[(__bridge id)[UIColor colorWithHex:0xedc474].CGColor,(__bridge id)[UIColor colorWithHex:0xff8560].CGColor];
    
    minTrackLayer.startPoint = CGPointMake(0.5, 1);
    
    minTrackLayer.endPoint = CGPointMake(0.5, 0);
    
    minTrackLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.minTrackLayer = minTrackLayer;
    [self.layer addSublayer:minTrackLayer];

}

- (void)setUpUI {
    CALayer *thumbImageLayer = [CALayer layer];
    UIImage  *thumbImage = [UIImage imageNamed:@"滑动"];

    thumbImageLayer.bounds = CGRectMake(0, 0, thumbImage.size.width, thumbImage.size.height);
    
    thumbImageLayer.position = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
    thumbImageLayer.contentsScale = [UIScreen mainScreen].scale;
    thumbImageLayer.contents = (__bridge id _Nullable)(thumbImage.CGImage);
    self.thumbImageLayer = thumbImageLayer;
    [self.layer addSublayer:thumbImageLayer];

    CGFloat postionY = CGRectGetHeight(self.frame) * self.value;
    
    if (postionY >= 0 && postionY <= self.bounds.size.height) {
        [self updateProgroess:postionY];
    }
}

- (void)setSliderValueAndCurrentTarget:(CGFloat)CurrentTarget {
    self.curretTarget = CurrentTarget;
    self.value = (self.curretTarget * UnitSpace(self.maxValue, self.sliderSpan)) / (100 * self.sliderSpan);
    
    [self updateProgroess:self.value * (CGRectGetHeight(self.frame))];
}

- (void)setSliderSpan:(CGFloat)span maxValue:(NSInteger)maxValue{
    self.maxValue = maxValue;
    self.sliderSpan = span;
}

@end
